class Projectile extends MonoBehaviour
{
	protected var player_target:GameCharacter;
	protected var enemy_target:EnemyCharacter;
	protected var _isToEnemy:boolean;
	protected var velocity:Vector3;
	protected var destination:Vector3;
	protected var _hasHit:boolean = false;
	protected var _speed = 0.1;
	protected var _willHit:boolean;
	protected var _projectileType:int;
	
	//private var particles:GameObject;
	
	public static var PEEKABOO:int = 1;
	public static var NORMAL:int =2;
	public static var SKILL:int = 3;
	public static var METEOR:int = 4;
	
	public function setup(source:GameCharacter, dest:EnemyCharacter, hit:boolean, projectileType:int)
	{
		_isToEnemy = true;
		player_target = source;
		enemy_target = dest;
		_projectileType = projectileType;
		
		//fire directly to target
		var sourceCoord = source.getSurfaceCoord();
		if (projectileType == METEOR)
			sourceCoord.y += 3;
		velocity = dest.getSurfaceCoord() - sourceCoord;
		velocity.Normalize();
		destination = dest.getSurfaceCoord();
		
		_willHit = hit;
		
		if (!hit)
		{
			//miss, random variation to the velocity
			velocity.x += Random.Range(-0.1,0.1);
			velocity.y += Random.Range(-0.1,0.1);
			velocity.z += Random.Range(-0.1,0.1);
			
			miss();
		}
		this.transform.position = sourceCoord;
		this.transform.rotation = Quaternion.LookRotation(velocity);
		
		
		/*var loadprefabs;
		var inverserotate = this.transform.rotation;
		inverserotate.x += 180;
		loadprefabs = Resources.Load("ProjectileTrail");
		particles = MonoBehaviour.Instantiate(loadprefabs,this.transform.position,Quaternion.Inverse(this.transform.rotation));*/
		
	}
	
	public function setup(source:EnemyCharacter, dest:GameCharacter, hit:boolean, projectileType:int)
	{
		_isToEnemy = false;
		player_target = dest;
		enemy_target = source;
		_projectileType = projectileType;
		
		//fire directly to target
		var sourceCoord = source.getSurfaceCoord();
		if (projectileType == METEOR)
			sourceCoord.y += 3;
			
		velocity = dest.getSurfaceCoord() - sourceCoord;
		velocity.Normalize();
		destination = dest.getSurfaceCoord();
		
		_willHit = hit;
		
		if (!hit)
		{
			//miss, random variation to the velocity
			velocity.x += Random.Range(-0.1,0.1);
			velocity.y += Random.Range(-0.1,0.1);
			velocity.z += Random.Range(-0.1,0.1);
			
			miss();
		}
		this.transform.position = sourceCoord;
		this.transform.rotation = Quaternion.LookRotation(velocity);

	}
	
	public function setSpeed(speed:float)
	{
		_speed = speed;
	}
	
	protected function miss()
	{		
		//show miss text
		var _playerPkbText = Resources.Load("Attackable_text");
		_playerPkbText = MonoBehaviour.Instantiate(_playerPkbText);
		
		if (_isToEnemy)
		{	
			_playerPkbText.transform.position = enemy_target.getTileReference().getSurfaceCoord();	
			projectileReachedEnemy();
		}
		else
		{
			_playerPkbText.transform.position = player_target.getTileReference().getSurfaceCoord();
			projectileReachedPlayer();
		}
		
		_playerPkbText.transform.position.y += 2;
		_playerPkbText.GetComponent(AttackableText).setAutoRemoveText("Miss!", 12, 2);
	}
	
	public function Update()
	{
		//reached destination
		if (_willHit && Vector3.Distance(this.transform.position, destination) <= 0.5)
		{
			if (!_hasHit)
			{
				_hasHit = true;
				this.transform.position = destination;
				
				setOffExplosion(_projectileType);
				
				if (_isToEnemy)
				{
					enemy_target.adjustCurrentHealth(-player_target.getActualDamage());
					projectileReachedEnemy();
				}
				else
				{
					player_target.adjustCurrentHealth(-enemy_target.getActualDamage());
					projectileReachedPlayer();
				}
				//Stop the particle system
				var ps = this.GetComponent("ParticleSystem");
				if( ps != null )
					ps.Stop();
				
				//particles.renderer.enabled = false;
				this.gameObject.SetActive(false);
			}
		}
		else
		{
			this.transform.position += velocity * _speed;
			//particles.transform.position = this.transform.position;
		}
	}
	
	private function setOffExplosion(explosionType:int)
	{
		//choose explosion type and size
		var currentDetonator;
		if (explosionType == METEOR)
		{
			currentDetonator = Resources.Load("HugeExplosion");
			currentDetonator.GetComponent("Detonator").size = 1.25; 
		}
		else if (explosionType == SKILL)
		{
			currentDetonator = Resources.Load("Explosion");
			currentDetonator.GetComponent("Detonator").size = 5; 
		}
		else if (explosionType == NORMAL || explosionType == PEEKABOO)
		{
			currentDetonator = Resources.Load("Explosion");
			currentDetonator.GetComponent("Detonator").size = 2.5; 
		}
		
		//var offsetSize = currentDetonator.GetComponent("Detonator").size;// 2;
		var exp : GameObject = Instantiate (currentDetonator, this.transform.position, Quaternion.identity);
		exp.GetComponent("Detonator").detail = 10;
		Destroy(exp, 5); 
	}
	
	private function projectileReachedPlayer()
	{

		Debug.Log("Projectile Reached");
		if (_projectileType == PEEKABOO)
			enemy_target.peekabooDone();
		else
			enemy_target.setProjectileReached(true);
	}
	
	private function projectileReachedEnemy()
	{
		Debug.Log("Projectile Reached");
		if (_projectileType == PEEKABOO)
			player_target.peekabooDone();
		else
			player_target.setProjectileReached(true);
	}
	

}